/***********************************************************************************************************************************
 *    Name: Bulk_Restore.sql
 *  Author: Frank Figearo — http://www.sqlnerd.me/ — frank@sqlnerd.me
 * Summary: Build the T-SQL to restore every backup in the server's backup history.
**/
USE master;
WITH b AS (SELECT DISTINCT database_name FROM msdb.dbo.backupset WHERE database_name NOT IN (N'master',N'model',N'msdb',N'ReportServer',N'ReportServerTempDB'))
SELECT N'
--==================================================================================================================================
PRINT ''' + database_name + N''';
GO
' + tsql_command AS [T-SQL Command]
  FROM b CROSS APPLY DBAdmin.BuildRestoreTSQL(database_name, DEFAULT, DEFAULT) brt
  ORDER BY database_name, ordinal;
GO